<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\LogAdmin;
use app\common\facade\admin\System as SystemModule;
use think\exception\ValidateException;

class Log extends BaseController
{
    /**
     * 日志列表
     */
    public function List()
    {
        $param = $this->request->get([
            'query',
            'types',
            'offset' => $this->request->get('pagenum/d')?:0,
            'length' => $this->request->get('pagesize/d')?:20
        ]);

        try {
            $data = LogAdmin::LogList($param);
            foreach ($data['log'] as $k => $val) {
                if ($val['type'] == 'sys') $data['log'][$k]['type_name'] = '系统日志';
                if ($val['type'] == 'mat') $data['log'][$k]['type_name'] = '素材操作日志';
                if ($val['type'] == 'pro') $data['log'][$k]['type_name'] = '节目单操作日志';
                if ($val['role_id'] == 0) $data['log'][$k]['adminRoleHasOne'] = ['name'=>'管理员'];
            }
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 获取当前用户日志
     */
    public function AuthUserLog() {
        $param = $this->request->get([
            'offset' => $this->request->get('page/d') ? $this->request->get('page/d') + 1 : 1,
            'length' => $this->request->get('size/d')?:10,
            'sort'
        ]);

        try {
            $logList = LogAdmin::AuthUserLog($param);
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($logList);
    }

    /**
     * 获取当前日志设置
     */
    public function logsetinfo()
    {
        try {
            $data = LogAdmin::getLogset();
        } catch(ValidateException $e) {
            return validateJson($e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode()); 
        }

        return successJson($data);
    }

    /**
     * 保存日志设置
     */
    public function logset()
    {
        $param = $this->request->post();
        try {
            $data = SystemModule::baseseting($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        
        return successJson($data);
    }

}